from google.appengine.api import images
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from photo import * 

import logging

class ImageJob(webapp.RequestHandler):
	def get(self):
		if self.request.get("url") and self.request.get("size"):
			size = self.request.get("size")
			url = self.request.get("url")
			q = db.GqlQuery("SELECT * from Photo where title = :1", url)
			result = q.fetch(1)
			self.response.headers['Content-Type'] = 'image/png'
			if len(result) == 0:
				saveImage(url)
				q = db.GqlQuery("SELECT * from Photo where title = :1", url)
				result = q.fetch(1)

			if len(result) > 0:
				photo = result[0]
				if size == "medium":
					self.response.out.write(photo.medium_size_image)
				else:
					self.response.out.write(photo.small_size_image)

application = webapp.WSGIApplication(
	[('/image', ImageJob)],
	debug=False)

def main():
	run_wsgi_app(application)

if __name__ == "__main__":
	main()
